#include  "fem.h"
#include  "legendre.h"
#include  <cassert>

/*----------------------------------------------------------*/

void Fem::initCell(double x1, double x2)
{
  _dx = x2-x1;
}

/*----------------------------------------------------------*/

void Fem::initIntegrationPoint(double chi)
{
  _chi = chi;
}

/*----------------------------------------------------------*/

void Fem::computeFunction(Vector& ulocal, int ic, const SystemVector& uglobal) const
{
  int degre = uglobal(0).degre();
  ulocal.zero();
  for(int icomp = 0; icomp < ulocal.size(); icomp++)
  {
    for(int i = 0; i < degre+1; i++)
    {
      ulocal[icomp] += phi(i)*uglobal(icomp)(ic, i);
    }
  }
}

/*----------------------------------------------------------*/

double Fem::phi(int i) const
{
  if(i == 0)
  {
    return legendren<0>(_chi);
  }
  else if(i == 1)
  {
    return legendren<1>(_chi);
  }
  else if(i == 2)
  {
    return legendren<2>(_chi);
  }
  else if(i == 3)
  {
    return legendren<3>(_chi);
  }
  else if(i == 4)
  {
    return legendren<4>(_chi);
  }
  else if(i == 5)
  {
    return legendren<5>(_chi);
  }
  assert(0);
}

/*----------------------------------------------------------*/

double Fem::dphi(int i) const
{
  if(i == 0)
  {
    return dlegendren<0>(_chi)/_dx;
  }
  else if(i == 1)
  {
    return dlegendren<1>(_chi)/_dx;
  }
  else if(i == 2)
  {
    return dlegendren<2>(_chi)/_dx;
  }
  else if(i == 3)
  {
    return dlegendren<3>(_chi)/_dx;
  }
  else if(i == 4)
  {
    return dlegendren<4>(_chi)/_dx;
  }
  else if(i == 5)
  {
    return dlegendren<5>(_chi)/_dx;
  }
  assert(0);
}

